package com.yoomiito.app.utils

import android.content.Context
import com.alibaba.fastjson.JSONObject
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.yoomiito.app.model.sdd.SddRights
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder

object ConfigFileUtils {

    /**
     * 获取送多多权限
     */
    fun sendMoreRights(context: Context):List<SddRights>{
        val str = getJsonFromFile(context,"config/send_rights.json")
        return Gson().fromJson(str,object : TypeToken<List<SddRights>>() {}.type)
//        return JSONObject.parseArray(str, SddRights::class.java)
    }

    private fun getJsonFromFile(context: Context,fileName:String):String{
        val sb = StringBuilder()
        val assetManager = context.assets
//        assetManager.open(fileName).buffered().
        val bf = BufferedReader(InputStreamReader(assetManager.open(fileName)))
        bf.use {
            var line:String
            while (true){
                line = it.readLine()?:break
                sb.append(line)
            }
        }
        return sb.toString()
//        var line: String?
//        while (bf.readLine().also { line = it } != null) {
//            sb.append(line)
//        }
//        return line!!

    }
}